package com.sun.algorithm.sparse;


//稀疏数组
public class sparse1 {
    public static void main(String[] args) {
        int[][] chess = new int[11][11];
        chess[1][2] = 1;
        chess[2][3] = 2;
        int sum = 0;
        //将二维数组转稀疏数组
        //1 遍历二维数组 得到非零数据的个数
        for (int[] ints : chess) {
            for (int anInt : ints) {
                System.out.print("\t"+anInt);
                if (anInt != 0){
                    sum++;
                }
            }
            System.out.println();
        }
        //创建对应的稀疏数组
        int sparseArr[][] = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;
        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chess[i][j] != 0){
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chess[i][j];
                }
            }
        }
        for (int[] ints : sparseArr) {
            for (int anInt : ints) {
                System.out.printf("%d\t",anInt);
            }
            System.out.println();
        }
    }
}
